package com.smt.modules.app.controller;

import java.util.Arrays;

import com.smt.common.enums.Code;
import com.smt.common.utils.*;
import com.smt.modules.app.annotation.BillAndGrade;
import com.smt.modules.app.annotation.Login;
import com.smt.modules.app.annotation.LoginUser;
import com.smt.modules.bus.entity.MemberUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.smt.common.base.BaseController;
import com.smt.modules.bus.vo.CompanyVo;
import com.smt.modules.bus.entity.CompanyEntity;
import com.smt.modules.bus.service.CompanyService;


/**
 * 公司信息表
 *
 * @author murong
 * @email 981620741@qq.com
 * @date 2018-09-29 15:37:09
 */
@RestController
@RequestMapping("app/company")
@Api(tags = {"AppCompanyController"},description = "公司信息表(客户端接口)")
public class AppCompanyController extends BaseController{
    @Autowired
    private CompanyService companyService;


    /**
    * 获取当前用户的公司列表 TODO
    *   需要传入项目id
     *
    * @return
    */
    @Login
    @GetMapping("/list")
    @ApiOperation(value = "/list",httpMethod = "获取当前用户的公司列表")
    public R list(@LoginUser MemberUserEntity memberUserEntity){
        //判断项目的从属者
        //获取项目创建者公司信息

        return companyService.queryCompanyList(memberUserEntity);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "/info/{id}",notes = "根据表ID查询",response = CompanyVo.class)
    public R info(@PathVariable("id") Long id){
			CompanyEntity company = companyService.selectById(id);

        return R.ok().put("company", company);
    }

    /**
     * 创建公司
     */
    @Login
    @PostMapping("/create")
    @ApiOperation(value = "创建公司",httpMethod = "POST")
    @BillAndGrade(value = Constant.TASK_6)
    public R create(@RequestBody CompanyVo company,
                    @LoginUser MemberUserEntity memberUserEntity){
        R r = companyService.create(company, memberUserEntity);
        return r;
    }

    /**
     * 修改
     */
    @Login
    @PostMapping("/update")
    @ApiOperation(value = "/update",notes = "更新信息保存",response = CompanyVo.class)
    @BillAndGrade(value = Constant.TASK_3)
    public R update(@RequestBody CompanyVo company,
                    @LoginUser MemberUserEntity memberUserEntity){
        //只有创建者才能修改公司简介
        if(memberUserEntity.getUserId().equals(company.getMemberId())){
            boolean b = companyService.updateById(company.copyPropertiesTo(new CompanyEntity()));
            return b?R.ok("修改成功"):R.error(EnumToKeyUtil.toInt(Code.MODIFY_DATA_ERROR),"公司信息修改失败");
        }else{
            return R.error(EnumToKeyUtil.toInt(Code.MISSING_PERMISSION),"您不是该公司的创建者");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "/delete",notes = "批量删除",response = CompanyVo.class)
    public R delete(@RequestBody Long[] ids){
			companyService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }



}
